package com.hepinghospital.user.ui.activity.home;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.adapter.GeneralScheduleAdapter;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.ui.PayActivity;
import com.hepinghospital.user.ui.activity.graphic.graphicSelect.GraphicSelectActivity;
import com.hepinghospital.user.ui.dialog.OffLineDialogFragment;
import com.hepinghospital.user.ui.dialog.RulesDialogFragment;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.Doctor;
import com.rest.response.TeamDetailResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.DpUtil;
import jz.joyoung.robot.util.UIConstants;
import jz.joyoung.robot.util.UtilList;

/**
 *
 * @author HP
 * @date 2020/2/26
 */

@Deprecated
public class DoctorDetailOffLineActivity extends BaseActivity {
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_level)
    TextView tv_level;
    @BindView(R.id.tv_hospital)
    TextView tv_hospital;
    @BindView(R.id.tv_skill)
    TextView tv_skill;
    @BindView(R.id.tv_info)
    TextView tv_info;
    @BindView(R.id.img_head)
    ImageView img_head;
    @BindView(R.id.tv_star)
    TextView tv_star;
    @BindView(R.id.btn_tuwen)
    TextView btn_tuwen;
    @BindView(R.id.btn_cloud)
    TextView btn_cloud;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.btn_more)
    TextView btnMore;
    @BindView(R.id.iv_doctor_photo)
    ImageView ivDoctorPhoto;
    @BindView(R.id.iv_doctor_cloud)
    ImageView ivDoctorCloud;
    @BindView(R.id.tv_show_one)
    TextView tvShowOne;
    @BindView(R.id.tv_show_two)
    TextView tvShowTwo;
    @BindView(R.id.tv_show_three)
    TextView tvShowThree;
    @BindView(R.id.tv_go)
    TextView tvGo;
    @BindView(R.id.scrollView)
    ScrollView scrollView;

    @BindView(R.id.schedule_container)
    RelativeLayout schedule_container;
    @BindView(R.id.tv_cloud_go)
    TextView tvCloudGo;
    @BindView(R.id.ratingBar)
    RatingBar ratingBar;
    String id;
    MyAdapter adapter;
    TeamAdapter teamAdapter;

    Doctor doctor;
    @BindView(R.id.team_recyclerView)
    RecyclerView team_recyclerView;
    @BindView(R.id.team_container)
    LinearLayout team_container;

    List<Doctor.Schedule> list = new ArrayList<>();
    List<Doctor.GeneralSchedule> listOffline = new ArrayList<>();
    List<TeamDetailResponse> teamList = new ArrayList<>();
    @BindView(R.id.iv_doctor_offline)
    ImageView ivDoctorOffline;

    private int chooseType = 1;
    private boolean isCloud = true;
    private boolean isPhoto = true;
    private boolean isWorkTime;
    private String feePhoto;
    private RulesDialogFragment mRulesDialogFragment;
    private GeneralScheduleAdapter generalScheduleAdapter;


    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(DoctorDetailOffLineActivity.this, 1);
            }
        });

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {

        });


        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        manager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        team_recyclerView.setLayoutManager(manager2);
        teamAdapter = new TeamAdapter();
        team_recyclerView.setAdapter(teamAdapter);
        team_recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = DpUtil.dip2px(DoctorDetailOffLineActivity.this, 1);
            }
        });
    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("id");
        getDoctorDetail();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_doctor_detail;
    }


    private void getDoctorDetail() {
        RestProxy.getInstance().getDoctorDetail(id, new Observer<Doctor>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Doctor doctorDetailResponse) {
                if (doctorDetailResponse.doctorTeamListDto.size() == 0) {
                    team_container.setVisibility(View.GONE);
                } else {
                    team_container.setVisibility(View.VISIBLE);
                    teamList.addAll(doctorDetailResponse.doctorTeamListDto);
                    teamAdapter.notifyDataSetChanged();
                }
                doctor = doctorDetailResponse;
                tv_name.setText(doctorDetailResponse.docName);
                tv_level.setText(doctorDetailResponse.deptName + " " + doctorDetailResponse.jobTitleName);
                tv_hospital.setText(doctorDetailResponse.hospitalName);
                tv_skill.setText("擅长疾病: " + doctorDetailResponse.docGoodAtNames);
                tv_info.setText("个人简介: " + doctorDetailResponse.introduction);
                tv_level.setText(doctorDetailResponse.jobTitleName);
                tv_star.setText(doctorDetailResponse.score);
                ratingBar.setRating(Float.parseFloat(doctorDetailResponse.score));
                ImageLoaderHelper.getInstance().GlideCircleImageLoader(DoctorDetailOffLineActivity.this, doctorDetailResponse.thumbnailUrl, img_head);
                feePhoto = doctorDetailResponse.fee;
//                tvGo.setText( "图文问诊预约¥" + feePhoto + "元/次" );

                if (!UtilList.isEmpty(doctorDetailResponse.generalScheduleList)) {
                    schedule_container.setVisibility(View.VISIBLE);
                    listOffline.clear();
                    listOffline.addAll(doctorDetailResponse.generalScheduleList);
                    generalScheduleAdapter.notifyDataSetChanged();
                } else {
                    schedule_container.setVisibility(View.GONE);
                }

                isWorkTime = !UtilList.isEmpty(doctorDetailResponse.scheduleInfoDtoList);

                list.clear();
                list.addAll(doctorDetailResponse.scheduleInfoDtoList);
                adapter.notifyDataSetChanged();

                //0:图文咨询和云门诊 1：图文咨询 3:云门诊 4：都不是
                if (doctorDetailResponse.diagType == 1) {
                    btn_cloud.setEnabled(false);
                    btn_cloud.setTextColor(getResources().getColor(R.color.commonGrey));
                    isCloud = false;
                    tvCloudGo.setVisibility(View.GONE);
                } else if (doctorDetailResponse.diagType == 3) {
                    btn_tuwen.setEnabled(false);
                    btn_tuwen.setTextColor(getResources().getColor(R.color.commonGrey));
                    isPhoto = false;
                } else if (doctorDetailResponse.diagType == 4) {
                    isPhoto = false;
                    isCloud = false;
                }

                tv_info.post(() -> {
                    Layout l = tv_info.getLayout();
                    if (l != null) {
                        int lines = l.getLineCount();
                        Log.d("lines =====", lines + "");
                        if (lines > 0) {
                            if (l.getEllipsisCount(lines - 1) > 0) {
                                btnMore.setVisibility(View.VISIBLE);
                            } else {
                                btnMore.setVisibility(View.GONE);
                            }
                        }
                    } else {
                        Log.d("999", "Layout is null");
                    }
                });
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                ToastUtil.onError(DoctorDetailOffLineActivity.this, e);
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);

            }
        });
    }


    @OnClick({R.id.iv_doctor_offline, R.id.iv_doctor_photo, R.id.iv_doctor_cloud, R.id.tv_go, R.id.tv_cloud_go, R.id.btn_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_doctor_offline:
                //选择预约挂号
                if (!UtilList.isEmpty(listOffline)) {
                    schedule_container.setVisibility(View.VISIBLE);
                } else {
                    schedule_container.setVisibility(View.GONE);
                }
                recyclerView.setVisibility(View.GONE);
                ivDoctorOffline.setImageDrawable(getDrawable(R.mipmap.icon_offline_press));
                ivDoctorPhoto.setImageDrawable(getDrawable(R.mipmap.icon_photo_normal));
                ivDoctorCloud.setImageDrawable(getDrawable(R.mipmap.icon_cloud_normal));
                tvShowOne.setText("预约医生门诊号源");
                tvShowTwo.setText("线上预约、院内取号就诊");
                tvShowThree.setText("预约前请仔细阅读挂号须知");
                tvGo.setVisibility(View.GONE);
                tvCloudGo.setVisibility(View.GONE);
                break;
            case R.id.iv_doctor_photo:
                //图文问诊
                ivDoctorOffline.setImageDrawable(getDrawable(R.mipmap.icon_offline_normal));
                ivDoctorPhoto.setImageDrawable(getDrawable(R.mipmap.icon_photo_press));
                ivDoctorCloud.setImageDrawable(getDrawable(R.mipmap.icon_cloud_normal));
                tvGo.setText("图文问诊预约¥" + feePhoto + "元/次");
                tvShowOne.setText("使用图片、文字与医生线上沟通");
                tvShowTwo.setText("医生24小时内未接诊自动全额退款");
                tvShowThree.setText("问诊服务需双方确认后结束");
                chooseType = 1;
                schedule_container.setVisibility(View.INVISIBLE);
                recyclerView.setVisibility(View.GONE);

                tvGo.setVisibility(View.VISIBLE);
                tvCloudGo.setVisibility(View.GONE);
                scrollView.fullScroll(ScrollView.FOCUS_UP);
                break;
            case R.id.iv_doctor_cloud:
                //选择云门诊
                if (isWorkTime) {
                    schedule_container.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);
                } else {
                    schedule_container.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.GONE);
                }
                ivDoctorOffline.setImageDrawable(getDrawable(R.mipmap.icon_offline_normal));
                ivDoctorPhoto.setImageDrawable(getDrawable(R.mipmap.icon_photo_normal));
                ivDoctorCloud.setImageDrawable(getDrawable(R.mipmap.icon_cloud_press));
                tvShowOne.setText("预约线上门诊号源与医生视频沟通就诊");
                tvShowTwo.setText("到达预约时间在APP上报道后，等待医生开启视频");
                tvShowThree.setText("视频未接通可申请退款");
                chooseType = 2;

                tvGo.setVisibility(View.GONE);
                tvCloudGo.setVisibility(View.VISIBLE);
//                scrollView.fullScroll( ScrollView.FOCUS_UP);
                break;
            case R.id.tv_go:
                if (!isPhoto) {
                    ToastUtil.showToast(mContext, "该医生没开启图文问诊预约");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable(UIConstants.OBJ_Doctor, doctor);
                bundle.putInt(UIConstants.PayActivity_TYPE_FLAG, UIConstants.PayActivity_TYPE_0);
                startActivity(GraphicSelectActivity.class, bundle);

                break;
            case R.id.tv_cloud_go:
                if (!isCloud || !isWorkTime) {
                    ToastUtil.showToast(mContext, "该医生没开启云门诊预约");
                    return;
                }

                Bundle bundleCloud = new Bundle();
                bundleCloud.putSerializable(UIConstants.OBJ_Doctor, doctor);
                bundleCloud.putInt(UIConstants.PayActivity_TYPE_FLAG, UIConstants.PayActivity_TYPE_1);
                startActivity(PayActivity.class, bundleCloud);
                break;
            case R.id.btn_more:
                if (tv_info.getMaxLines() == 2) {
                    btnMore.setText("点击收起>");
                    tv_info.setMaxLines(100);
                } else {
                    btnMore.setText("点击展开>");
                    tv_info.setMaxLines(2);
                }
        }
    }


    /**
     * 预约日期选择
     *
     * @time 2020/4/20 9:24
     */
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(DoctorDetailOffLineActivity.this).inflate(R.layout.item_schedule, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.tv_scheduleTime.setText(list.get(position).scheduleTime);
            holder.tv_dateTime.setText(list.get(position).dateTime);
            holder.tv_regFee.setText("¥" + list.get(position).regFee);
            if (list.get(position).isSchedule == 0) {
                holder.btn_confirm.setText("已约满");
//                holder.btn_confirm.setBackgroundResource( R.drawable.shape_grey );
                holder.btn_confirm.setTextColor(Color.parseColor("#ffd5d5d5"));
            } else {
                holder.btn_confirm.setText("可预约");
//                holder.btn_confirm.setBackgroundResource( R.drawable.selector_button );
                holder.btn_confirm.setTextColor(Color.parseColor("#ff209187"));
            }
            holder.btn_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_scheduleTime, tv_dateTime, tv_regFee, btn_confirm;

            public Holder(View itemView) {
                super(itemView);
                tv_scheduleTime = itemView.findViewById(R.id.tv_scheduleTime);
                tv_dateTime = itemView.findViewById(R.id.tv_dateTime);
                tv_regFee = itemView.findViewById(R.id.tv_regFee);
                btn_confirm = itemView.findViewById(R.id.btn_confirm);
            }
        }
    }


    class TeamAdapter extends RecyclerView.Adapter<TeamAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(DoctorDetailOffLineActivity.this).inflate(R.layout.item_team, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.tv_name.setText(teamList.get(position).name);
            holder.tv_detail.setText(teamList.get(position).description);
            ImageLoaderHelper.getInstance().GlideCircleImageLoader(DoctorDetailOffLineActivity.this, teamList.get(position).thumbnailUrl, holder.img_head);
            holder.itemView.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putString("id", teamList.get(position).id + "");
                startActivity(TeamDetailActivity.class, bundle);
            });
        }

        @Override
        public int getItemCount() {
            return teamList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_detail;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_detail = itemView.findViewById(R.id.tv_detail);
                img_head = itemView.findViewById(R.id.img_head);
            }
        }
    }

    @SuppressLint("NonConstantResourceId")
    @OnClick(R.id.btn_tuwen)
    void btn_tuwen() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(UIConstants.OBJ_Doctor, doctor);
        bundle.putInt(UIConstants.PayActivity_TYPE_FLAG, UIConstants.PayActivity_TYPE_0);
        startActivity(GraphicSelectActivity.class, bundle);
    }

    @OnClick(R.id.btn_cloud)
    void btn_cloud() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(UIConstants.OBJ_Doctor, doctor);
        bundle.putInt(UIConstants.PayActivity_TYPE_FLAG, UIConstants.PayActivity_TYPE_1);
        startActivity(PayActivity.class, bundle);
    }

    /**
     * 查询规则
     *
     * @time 2020/3/18 14:41
     */
    @OnClick(R.id.schedule_container)
    void queryProtocol() {

        showRulesDialogFragment();
    }

    private void showRulesDialogFragment() {

        mRulesDialogFragment = RulesDialogFragment.newInstance("6");
        mRulesDialogFragment.setmListener(new RulesDialogFragment.OnItemClickListener() {


            @Override
            public void onConfirm() {
                mRulesDialogFragment.dismiss();
            }
        });
        mRulesDialogFragment.show(getSupportFragmentManager(), "mSchudeleDialogFragment");
    }
}
